package cn.edu.tju.elm.model;

import cn.edu.tju.core.model.BaseEntity;
import jakarta.persistence.Entity;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import java.math.BigDecimal;

@Entity
@Table(name = "orderdetailet")
public class OrderDetailet extends BaseEntity {
    @ManyToOne
    @JoinColumn(name = "order_id", nullable = false)
    private Order order;

    @ManyToOne
    @JoinColumn(name = "food_id", nullable = false)
    private Food food;

    private Integer quantity;

    // 添加快照字段 - 保存下单时的商品信息
    private String foodNameSnapshot;      // 商品名称快照
    private BigDecimal foodPriceSnapshot; // 商品价格快照
    private String foodImgSnapshot;       // 商品图片快照
    private String foodExplainSnapshot;   // 商品描述快照
    private BigDecimal deliverPrinceSnapshot;

    public void setDeliverPrinceSnapshot(BigDecimal deliverPrinceSnapshot) {
        this.deliverPrinceSnapshot = deliverPrinceSnapshot;
    }

    public BigDecimal getDeliverPrinceSnapshot() {
        return deliverPrinceSnapshot;
    }

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }

    public Food getFood() {
        return food;
    }

    public void setFood(Food food) {
        this.food = food;
    }

    public Integer getQuantity() {
        return quantity;
    }

    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }

    // 快照字段的getter和setter
    public String getFoodNameSnapshot() {
        return foodNameSnapshot;
    }

    public void setFoodNameSnapshot(String foodNameSnapshot) {
        this.foodNameSnapshot = foodNameSnapshot;
    }

    public BigDecimal getFoodPriceSnapshot() {
        return foodPriceSnapshot;
    }

    public void setFoodPriceSnapshot(BigDecimal foodPriceSnapshot) {
        this.foodPriceSnapshot = foodPriceSnapshot;
    }

    public String getFoodImgSnapshot() {
        return foodImgSnapshot;
    }

    public void setFoodImgSnapshot(String foodImgSnapshot) {
        this.foodImgSnapshot = foodImgSnapshot;
    }

    public String getFoodExplainSnapshot() {
        return foodExplainSnapshot;
    }

    public void setFoodExplainSnapshot(String foodExplainSnapshot) {
        this.foodExplainSnapshot = foodExplainSnapshot;
    }

    // 便捷方法：获取显示用的商品信息（优先使用快照）
    public String getDisplayFoodName() {
        return foodNameSnapshot != null ? foodNameSnapshot : (food != null ? food.getFoodName() : "");
    }

    public BigDecimal getDisplayFoodPrice() {
        return foodPriceSnapshot != null ? foodPriceSnapshot : (food != null ? food.getFoodPrice() : BigDecimal.ZERO);
    }

    public String getDisplayFoodImg() {
        return foodImgSnapshot != null ? foodImgSnapshot : (food != null ? food.getFoodImg() : "");
    }

    public String getDisplayFoodExplain() {
        return foodExplainSnapshot != null ? foodExplainSnapshot : (food != null ? food.getFoodExplain() : "");
    }
}